package com.activity;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;

import com.type.Topic;

public class FriendTopicActivity extends ListActivity {
	// constant
	public static final String ACTIVITY_INDEX = "activity_index";
	static final int FETCH_FRIEND_CREATED_ACTIVITY_LIST = 4;
	static final String PHONE_NUMBER_OF_FRIEND_TO_FETCH = "phone_number_of_friend_to_fetch";
	static final String FETCH_FRIEND_CREATED_ACTIVIY_COMPLETE_ACTION = "fetch_friend_created_activiy_complete_action";
	static final String FRIEND_CREATED_ACTIVITY_LIST = "com.activity.FRIEND_CREATED_ACTIVITY_LIST";
	static final int COMPLETE_FETCH_FRIEND_CREATED_ACTIVIY_LIST = 1;
	static final String FRIEND_INDEX = "friend_index";

	// field
	long myPhoneNum;
	long friendPhoneNum;
	ArrayList<Topic> activityList= new ArrayList<Topic>();
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
		
		myPhoneNum = getIntent().getLongExtra(MainActivity.MY_PHONE_NUMBER, 0);
		friendPhoneNum = getIntent().getLongExtra(MainActivity.FRIEND_PHONE_NUMBER, 0);
		fetchCreatedActivityList();
	}

	
	void fetchCreatedActivityList() {
		new Thread(new Runnable() {
			@Override
			public void run() {
				Socket socket = null;
				DataOutputStream dos = null;
				DataInputStream dis = null;
				try {
					socket = new Socket(MainActivity.SERVER_IP, 9999);
					dos = new DataOutputStream(socket.getOutputStream());
					dis = new DataInputStream(socket.getInputStream());
					dos.writeInt(FETCH_FRIEND_CREATED_ACTIVITY_LIST);
					dos.writeLong(friendPhoneNum);
					int activityCount = dis.readInt();
					if (activityCount > 0) {
						for (int i = 0; i < activityCount; i++) {
							Topic topic = new Topic();
							topic.receive(dis);
							activityList.add(topic);
						}
					}
					mHandler.sendEmptyMessageDelayed(COMPLETE_FETCH_FRIEND_CREATED_ACTIVIY_LIST, 0);
				} catch (IOException e) {
					e.printStackTrace();
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					try {
						dis.close();
						dos.close();
						socket.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}).start();
	}

	
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message message) {
			if(message.what == COMPLETE_FETCH_FRIEND_CREATED_ACTIVIY_LIST) {
				List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
				for(Topic topic : activityList) {
					Map<String, Object> map = new HashMap<String, Object>();
					map.put("activity_photo", topic.fetchBitmap());
					map.put("activity_title", topic.getTitle());
					map.put("activity_content", topic.getContent());
					list.add(map);
				}
				SimpleAdapter adapter = new SimpleAdapter(FriendTopicActivity.this, 
						list,
						R.layout.temp_friend_detail_created_activity_list_item, 
						new String[]{"activity_photo", "activity_title", "activity_content"}, 
						new int[]{R.id.ivActivityPhoto, R.id.tvActivityTitle, R.id.tvActivityContent});
				adapter.setViewBinder(new ViewBinder() {
					@Override
					public boolean setViewValue(View view, Object data, String textRepresentation) {
						if ((view instanceof ImageView) && (data instanceof Bitmap)) {
							ImageView iv = (ImageView) view;
							Bitmap bm = (Bitmap) data;
							iv.setImageBitmap(bm);
							return true;
						} else
							return false;
					}
				});
				
				getListView().setAdapter(adapter);
				getListView().setOnItemClickListener(new OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
						Intent intent = new Intent(FriendTopicActivity.this, FriendTopicFollowDetailActivity.class);
						// TODO zyc must handle below:
						intent.putExtra(MainActivity.MY_PHONE_NUMBER, myPhoneNum);
						intent.putExtra(MainActivity.FRIEND_PHONE_NUMBER, friendPhoneNum);
						intent.putExtra(ACTIVITY_INDEX, position);
						startActivity(intent);
					}
				});
			}
		}
	};
}
